using System;
using System.Collections;
using BCW;
using I2.Loc;
using UnityEngine;

public class GotchaShowButton : MonoBehaviour
{
	public static GotchaShowButton instance;

	public UIBasicSprite texture;

	public UILabel timer;

	public UILabel info;

	private GotchaChest currentChest;

	private Coroutine timerCoroutine;

	private bool coroutineRunning;

	private void OnClick()
	{
		if (!JSONManager.Instance.ServerTimeLoad())
		{
			controllerMenu.thisScript.ShowNoInternet();
		}
		else if (!coroutineRunning)
		{
			coroutineRunning = true;
			CoroutineRunner.Instance.StartCoroutine(ShowGotchaUI());
		}
	}

	private void Awake()
	{
		instance = this;
		texture = texture.ReplaceForCurrentPlatform();
	}

	private void Start()
	{
		SetUI();
	}

	private void OnChangeLanguageChestNoInternet()
	{
		info.text = ScriptLocalization.Get("Game_Common/chest_no_internet");
	}

	public void SetUI()
	{
		CoroutineRunner.Instance.StartCoroutine(FindCestForDisplay());
	}

	private IEnumerator FindCestForDisplay()
	{
		if (!JSONManager.Instance.ServerTimeLoad())
		{
			info.gameObject.SetActive(true);
			timer.gameObject.SetActive(false);
			texture.LoadTextureAndAlphaMask("AtlasShopGuns/", "Disabled_Сhest_" + GotchaChestType.Wooden);
			info.text = ScriptLocalization.Get("Game_Common/chest_no_internet");
			SwitchLanguage.onChangeLanguage += OnChangeLanguageChestNoInternet;
		}
		while (!JSONManager.Instance.ServerTimeLoad())
		{
			yield return null;
		}
		currentChest = null;
		foreach (GotchaChest chest2 in GotchaManager.instance.allCests)
		{
			if (chest2.IsUnlocked())
			{
				currentChest = chest2;
				RefreshUI();
				yield break;
			}
		}
		int unlockTime = int.MaxValue;
		foreach (GotchaChest chest in GotchaManager.instance.allCests)
		{
			if (chest.count > 0 && chest.unlockTime > 0 && chest.unlockTime < unlockTime)
			{
				unlockTime = chest.unlockTime;
				currentChest = chest;
			}
		}
		RefreshUI();
	}

	private void OnChangelanguageChestIsOpen()
	{
		info.text = string.Format(ScriptLocalization.Get("Game_Common/chest_is_open"), ScriptLocalization.Get("Game_Common/" + currentChest.type));
	}

	public void RefreshUI()
	{
		if (currentChest == null)
		{
			info.gameObject.SetActive(false);
			timer.gameObject.SetActive(false);
			texture.LoadTextureAndAlphaMask("AtlasShopGuns/", "Disabled_Сhest_" + GotchaChestType.Wooden);
			return;
		}
		if (currentChest.IsUnlocked())
		{
			if (info != null && timer != null)
			{
				info.gameObject.SetActive(true);
				timer.gameObject.SetActive(false);
			}
			texture.LoadTextureAndAlphaMask("AtlasShopGuns/", "Open_Сhest_" + currentChest.type);
			info.text = string.Format(ScriptLocalization.Get("Game_Common/chest_is_open"), ScriptLocalization.Get("Game_Common/" + currentChest.type));
			SwitchLanguage.onChangeLanguage += OnChangelanguageChestIsOpen;
		}
		else
		{
			info.gameObject.SetActive(false);
			timer.gameObject.SetActive(true);
			texture.LoadTextureAndAlphaMask("AtlasShopGuns/", "Closed_Сhest_" + currentChest.type);
		}
		UpdateTimer();
	}

	public void UpdateTimer()
	{
		if (timerCoroutine != null)
		{
			CoroutineRunner.Instance.StopCoroutine(timerCoroutine);
			timerCoroutine = null;
		}
		if (!currentChest.IsUnlocked())
		{
			timerCoroutine = CoroutineRunner.Instance.StartCoroutine(TimerIEnumerator(currentChest.unlockTime - JSONManager.Instance.GetServerTime()));
		}
	}

	private IEnumerator TimerIEnumerator(int secondsToOpen)
	{
		for (int currentTimer = secondsToOpen; currentTimer > 0; currentTimer--)
		{
			TimeSpan t = TimeSpan.FromSeconds(currentTimer);
			string time = ((t.Hours > 0) ? ((t.Hours <= 9) ? string.Format("{0:D1}:{1:D2}:{2:D2}", t.Hours, t.Minutes, t.Seconds) : string.Format("{0:D2}:{1:D2}:{2:D2}", t.Hours, t.Minutes, t.Seconds)) : ((t.Minutes > 0) ? ((t.Minutes <= 9) ? string.Format("{0:D1}:{1:D2}", t.Minutes, t.Seconds) : string.Format("{0:D2}:{1:D2}", t.Minutes, t.Seconds)) : ((t.Seconds <= 9) ? string.Format("{0:D1}", t.Seconds) : string.Format("{0:D2}", t.Seconds))));
			timer.text = time;
			yield return new WaitForSeconds(1f);
		}
		timer.text = "0";
		yield return new WaitForSeconds(1f);
		RefreshUI();
	}

	private IEnumerator ShowGotchaUI()
	{
		if (GotchaManagerUI.instance == null)
		{
			CoroutineRunner.Instance.StartCoroutine(GotchaManagerUI.CreateInstance());
		}
		while (GotchaManagerUI.instance == null)
		{
			yield return null;
		}
		GotchaManagerUI.instance.ShowGotchaUI();
		coroutineRunning = false;
	}
}
